import {Component, OnDestroy, OnInit} from '@angular/core';
import {FormBuilder} from '@angular/forms';
import {DatePipe} from '@angular/common';
import {DeviceCategoryEnum} from '../../common/device-tag-selector/device-category.enum';
import {Device} from '../../../entity/device.model';
import {DeviceGroupCategoryEnum} from '../../common/device-tag-selector/device-selector/device-selector.component';

@Component({
  selector: 'app-alarm-statistics',
  templateUrl: './alarm-statistics.component.html',
  styleUrls: ['./alarm-statistics.component.css']
})
export class AlarmStatisticsComponent implements OnInit, OnDestroy {
  public deviceCategories: DeviceCategoryEnum[] = [DeviceCategoryEnum.turbine, DeviceCategoryEnum.inverter];
  /**
   * 风机id集合
   *
   * @type @memberof AlarmSearchComponent
   */
  private turbineIds: number[] = [];

  /**
   * 查询时间formGroup；
   *
   * @type @memberof AlarmSearchComponent
   */
  public startTime = new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 7);
  public endTime = new Date(new Date().getTime());
  private fomat: string = 'yyyy.MM.dd';

  /**
   * 是否显示提示框
   *
   * @type {number}
   * @memberof AlarmSearchComponent
   */
  display: boolean = false;
  mes: string;

  /**
   * 查询时间间隔
   *
   * @type {number}
   * @memberof AlarmSearchComponent
   */
  public timeInterval: number;
  /**
   * 故障描述模糊查询
   *
   * @memberof AlarmSearchComponent
   */
  public divFaultDesc: string;
  /**
   *风机最大选择数量
   *
   * @type {number}
   * @memberof AlarmSearchComponent
   */
  public deviceCountLimit: number;
  /**
   *刚进页面是默认选择风机数量
   *默认:null 不做限制
   * 0不选择
   * @memberof AlarmSearchComponent
   */
  defaultChooseDeviceCount;
  isClearSelectDevice = false;
  en: any;
  /**
   *设备选择分组默认选中
   *
   * @type {DeviceGroupCategoryEnum}
   * @memberof AlarmSearchComponent
   */
  public deviceGroupCategory: DeviceGroupCategoryEnum = DeviceGroupCategoryEnum.byCircuit;

  constructor(
    private fb: FormBuilder,
    private datePipe: DatePipe
  ) {
  }


  ngOnInit(): void {
    this.en = {
      firstDayOfWeek: 0,
      dayNames: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
      dayNamesShort: ['日', '一', '二', '三', '四', '五', '六'],
      dayNamesMin: ['日', '一', '二', '三', '四', '五', '六'],
      monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
      monthNamesShort: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'],
      today: '今天',
      clear: '清除'
    };
    var startTime = this.datePipe.transform(this.startTime, this.fomat);
    var endTime = this.datePipe.transform(this.endTime, this.fomat);
    // this.onSearch()
  }

  ngOnDestroy(): void {
  }

  /**
   * 得到选中风场风机集合
   *
   * @type {Device[]}@memberof TurbineDataListComponent
   */
  onSelectedDevicesChange(ev: Device[]) {
    this.turbineIds = [];
    this.turbineIds.push(...ev.map(o => o.deviceId));
  }

  onSearch() {
    if (this.turbineIds.length <= 0) {
      alert('请选择风机');
      return;
    }
    if (this.endTime > new Date()) {
      this.mes = `结束时间不能大于当前时间`;
      return this.display = true;
    }
    if (this.startTime > this.endTime) {
      this.mes = `起始时间不能大于结束时间`;
      return this.display = true;
    }
    this.startTime = this.startTime;
    this.endTime = this.endTime;
  }
}
